home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_016 / source.files / raw2ilbm.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  196 lines

  1. /** raw2ilbm.c **************************************************************
  2. /*  Read in a "raw" bitmap (dump of the bitplanes in a screen)  */
  3. /*  Display it, and write it out as an ILBM file.        */
  4. /*  23-Jan-86                            */
  5. /*                                */
  6. /*  Usage from CLI: 'Raw2ILBM  source dest fmt(low,med,hi)     */
  7. /*     nplanes'                            */
  8. /*  Supports the three common Amiga screen formats.          */
  9. /*         'low' is 320x200,                */
  10. /*          'med' is 640x200,                 */
  11. /*         'hi' is 640x400.                  */
  12. /*         'nplanes' is the number of bitplanes.        */
  13. /*  The default is low-resolution, 5 bitplanes             */
  14. /*         (32 colors per pixel).                */
  15. /*                                                              */
  16. /* By Jerry Morrison and Steve Shaw, Electronic Arts.           */
  17. /* This software is in the public domain.                       */
  18. /*                                                              */
  19. /* This version for the Commodore-Amiga computer.               */
  20. /*                                                              */
  21. /****************************************************************/
  22.  
  23. #include "iff/intuall.h"
  24. #include "libraries/dos.h"
  25. #include "libraries/dosextens.h"
  26. #include "iff/ilbm.h"
  27. #include "iff/putpict.h"
  28.  
  29. #define MIN(a,b) ((a)<(b)?(a):(b))
  30. #define MAX(a,b) ((a)>(b)?(a):(b))
  31.  
  32. /* general usage pointers */
  33. LONG IconBase;    /* Actually, "struct IconBase *" if you've got some ".h" file*/
  34. struct GfxBase *GfxBase;
  35.  
  36.  
  37. /* Globals for displaying an image */
  38. struct RastPort rP;
  39. struct RasInfo rasinfo;
  40. struct View v = {0};
  41. struct ViewPort vp = {0};
  42. struct View *oldView = 0;    /* so we can restore it */
  43.  
  44. /* ---------------------------------- */
  45. DisplayPic(bm, colorMap) struct BitMap *bm; UWORD *colorMap;  {
  46.  
  47.     oldView = GfxBase->ActiView;    /* so we can restore it */
  48.         
  49.     InitView(&v);
  50.     InitVPort(&vp);
  51.     v.ViewPort = &vp;
  52.     InitRastPort(&rP);
  53.     rP.BitMap = bm;
  54.     rasinfo.BitMap = bm;
  55.  
  56.     /* Always show the upper left-hand corner of this picture. */
  57.     rasinfo.RxOffset = 0;
  58.     rasinfo.RyOffset = 0;
  59.  
  60.     vp.DWidth = bm->BytesPerRow*8;    /* Physical display WIDTH */
  61.     vp.DHeight = bm->Rows;    /* Display height */
  62.  
  63.     /* Always display it in upper left corner of screen.*/
  64.  
  65.     if (vp.DWidth <= 320) vp.Modes = 0;
  66.     else vp.Modes = HIRES;
  67.     if (vp.DHeight > 200) {
  68.     v.Modes |= LACE;
  69.     vp.Modes |= LACE;
  70.     }
  71.     vp.RasInfo = &rasinfo;
  72.     MakeVPort(&v,&vp);
  73.     MrgCop(&v);
  74.     LoadView(&v);    /* show the picture */
  75.     WaitBlit();
  76.     WaitTOF();
  77.     if (colorMap) LoadRGB4(&vp, colorMap,(1 << bm->Depth));
  78.     }
  79.  
  80. UnDispPict() {
  81.     if (oldView) {
  82.     LoadView(oldView);    /* switch back to old view */
  83.     FreeVPortCopLists(&vp);
  84.     FreeCprList(v.LOFCprList);
  85.     }
  86.     }
  87.  
  88. PrintS(msg)  char *msg; {   printf(msg);    }
  89.  
  90. void GoodBye(msg)  char *msg; {   PrintS(msg);   PrintS("\n");   exit(0);   }
  91.  
  92. struct BitMap bitmap = {0};
  93. SHORT cmap[32];
  94.  
  95. AllocBitMap(bm) struct BitMap *bm; {
  96.     int i;
  97.     LONG psz = bm->BytesPerRow*bm->Rows;
  98.     UBYTE *p = (UBYTE *)AllocMem(bm->Depth*psz, MEMF_CHIP|MEMF_PUBLIC);
  99.     for (i=0; i<bm->Depth; i++)  { 
  100.     bm->Planes[i] = p;
  101.     p += psz;
  102.     }
  103.     }
  104.  
  105. FreeBitMap(bm) struct BitMap *bm;  {
  106.     if (bitmap.Planes[0])  {
  107.     FreeMem(bitmap.Planes[0],
  108.         bitmap.BytesPerRow * bitmap.Rows * bitmap.Depth);
  109.     }
  110.     }
  111.  
  112. BOOL LoadBitMap(file,bm,cols)
  113.     LONG file;    
  114.     struct BitMap *bm;
  115.     SHORT *cols;
  116.     {
  117.     SHORT i;
  118.     LONG nb,plsize;
  119.     plsize = bm->BytesPerRow*bm->Rows;
  120.     for (i=0; i<bm->Depth; i++) {
  121.     nb =  Read(file, bm->Planes[i], plsize);
  122.     if (nb<plsize) BltClear(bm->Planes[i],plsize,1);
  123.     }
  124.     if (cols) {
  125.     nb = Read(file, cols, (1<<bm->Depth)*2);     /* load color map */
  126.     return( (BOOL) (nb == (1<<bm->Depth)*2) );
  127.     }
  128.     return((BOOL) FALSE);
  129.     }
  130.  
  131.  
  132. /** main() ******************************************************************/
  133.  
  134. UBYTE defSwitch[] = "b";
  135.     
  136. #define BUFSIZE  16000
  137.  
  138. static SHORT maxDepth[3] = {5,4,4};
  139.  
  140. void main(argc, argv)  int argc;  char **argv;  {
  141.     SHORT fmt,depth,pwidth,pheight;
  142.     UBYTE *buffer;
  143.     BOOL hadCmap;
  144.     LONG file;
  145.     if( !(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)) )
  146.     GoodBye("No graphics.library");
  147.     if( !(IconBase = OpenLibrary("icon.library",0)) )
  148.     GoodBye("No icon.library");
  149.     if (argc) {
  150.     if (argc < 3) {
  151.         printf(
  152. "Usage from CLI: 'Raw2ILBM  source dest fmt(low,med,hi) nplanes'\n");
  153.         goto bailout;
  154.         }
  155.     fmt = 0;
  156.     depth = 5;
  157.     if (argc>3)
  158.         switch(*argv[3]) {
  159.         case 'l': fmt = 0; break;
  160.         case 'm': fmt = 1; break;
  161.         case 'h': fmt = 2; break;
  162.         }
  163.     if (argc>4) depth = *argv[4]-'0';
  164.     depth = MAX(1, MIN(maxDepth[fmt],depth));
  165.     pwidth = fmt? 640: 320;
  166.     pheight = (fmt>1)? 400: 200;
  167.     InitBitMap(&bitmap, depth, pwidth, pheight);
  168.     AllocBitMap(&bitmap);
  169.     
  170.     file = Open(argv[1], MODE_OLDFILE);
  171.     
  172.     if (file)  { 
  173.         DisplayPic(&bitmap,NULL);
  174.         hadCmap = LoadBitMap(file,&bitmap, cmap);    
  175.         if (hadCmap) LoadRGB4(&vp, cmap, 1<<bitmap.Depth);
  176.         Close(file);
  177.         file = Open(argv[2], MODE_NEWFILE);
  178.         buffer = (UBYTE *)AllocMem(BUFSIZE, MEMF_CHIP|MEMF_PUBLIC);
  179.         PutPict(file, &bitmap, pwidth, pheight,
  180.         hadCmap? cmap: NULL, buffer, BUFSIZE);
  181.         Close(file);
  182.         FreeMem(buffer,BUFSIZE);
  183.         }
  184.     else printf(" Couldn't open file '%s' \n",argv[2]);
  185.     }
  186.  
  187.     UnDispPict();
  188.     FreeBitMap(&bitmap);
  189.     
  190.     bailout:
  191.     CloseLibrary(GfxBase);
  192.     CloseLibrary(IconBase);
  193.     exit(0);
  194.     }
  195.  
  196.